home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSKBD.C < prev    next >
C/C++ Source or Header  |  1995-06-07  |  6KB  |  282 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bskbd.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to read a keyboard. It is fairly specific to DOS,
  25. //    but it will work (with some restrictions) under UNIX.
  26. //
  27. //    The code in this module should be written entirely in C. 
  28. //    Do not use any C++ constructs.
  29. //
  30. //    This module is portable to:
  31. //        DOS 3.X+
  32. //        MS Windows 3.X+
  33. //        OS/2 2.X+
  34. //        OS/2 2.0 PM
  35. //        SCO UNIX.
  36. //
  37. //    The following compilers are supported:
  38. //        MSC 6.0A
  39. //        MSC/C++ 7.0
  40. //        Borland C++ 3.1 for DOS
  41. //        Borland C++ 1.0 for OS/2 2.X
  42. //        SCO UNIX cc
  43. //
  44. //----------------------------------------------------------------------------
  45. #include <bs.h>
  46.  
  47.  
  48. //----------------------------------------------------------------------------
  49. //    Globals
  50. //----------------------------------------------------------------------------
  51. #if OS_DOS
  52. static SHORT sRead   = 0x00;
  53. static SHORT sReady  = 0x01;
  54. static SHORT sState  = 0x02;
  55. static BOOL fExtended =  FALSE;
  56. #endif
  57.  
  58. #if OS_UNIX
  59. #    ifndef getch
  60. #        define getch()  getc(stdin)
  61. #    endif
  62. #endif
  63.  
  64.  
  65. //----------------------------------------------------------------------------
  66. //   Description:    Read a character from the keyboard.
  67. //    Parameters:
  68. //       Returns:    Character read from keyboard
  69. //----------------------------------------------------------------------------
  70. INT FN_EA KbdChar(void)
  71. {
  72. #if OS_DOS
  73.  
  74.     SHORT    sKey;
  75.     SHORT _sRead = sRead;
  76.     _asm {
  77.         mov    ah, BYTE PTR [_sRead]
  78.         int    16h
  79.         mov    [sKey], ax
  80.         }
  81.  
  82.     if ((sKey & 0x00FF) == 0x00E0)
  83.         sKey &= 0xFF00;
  84.     else if (sKey & 0x00FF)
  85.         sKey &= 0x00FF;
  86.  
  87.     return sKey;
  88.  
  89. #elif OS_PM || OS_WINDOWS
  90.  
  91.     return 0;
  92.  
  93. #elif OS_OS2
  94.  
  95.     INT ch = (INT)getch();
  96.     if (ch == 0)
  97.         {
  98.         ch = (INT)getch();
  99.         ch <<= 8;
  100.         }
  101.     return ch;
  102.  
  103. #else
  104.  
  105.     INT ch = (INT)getchar();
  106.     if (ch == 0)
  107.         {
  108.         ch = (INT)getchar();
  109.         ch <<= 8;
  110.         }
  111.     return ch;
  112.  
  113. #endif
  114. }
  115.  
  116.  
  117. //----------------------------------------------------------------------------
  118. //   Description:    Flush the keyboard.
  119. //    Parameters:
  120. //       Returns:    void
  121. //----------------------------------------------------------------------------
  122. VOID FN_E KbdFlush(void)
  123. {
  124.     while (KbdReady())
  125.         KbdChar();
  126.  
  127.     return ;
  128. }
  129.  
  130.  
  131. //----------------------------------------------------------------------------
  132. //   Description:    Initialize the keyboard interface.
  133. //    Parameters:    void
  134. //       Returns:    TRUE if successful.
  135. //----------------------------------------------------------------------------
  136. VOID FN KbdInitialize(void)
  137. {
  138. #if OS_DOS
  139.     fExtended = ((*(PUSHORT)(0x00400096L)) & 0x10);
  140.     sRead      = (fExtended) ? 0x10: 0x00;
  141.     sReady  = (fExtended) ? 0x11: 0x01;
  142.     sState  = (fExtended) ? 0x12: 0x02;
  143.     return ;
  144. #endif
  145. }
  146.  
  147.  
  148. //----------------------------------------------------------------------------
  149. //   Description:    Check keyboard insert state
  150. //    Parameters:    
  151. //       Returns:    TRUE if insert mode is on
  152. //----------------------------------------------------------------------------
  153. BOOL FN_E KbdIsInsert(void)
  154. {
  155. #if OS_UNIX || OS_PM || OS_WINDOWS
  156.     return FALSE;
  157. #else
  158.     return (KbdState()) != 0;   // added visual c++
  159. //    return (KbdState() & INSERT_ON) != 0;
  160. #endif
  161. }
  162.  
  163.  
  164. //----------------------------------------------------------------------------
  165. //   Description:    Check if a keystroke is waiting to be read.
  166. //    Parameters:    void
  167. //       Returns:    Keystroke waiting to be read or 0.
  168. //----------------------------------------------------------------------------
  169. INT FN_EA KbdReady(void)
  170. {
  171. #if OS_DOS
  172.  
  173.     SHORT    sKey;
  174.     SHORT _sReady = sReady;
  175.     _asm {
  176.         mov    ah, BYTE PTR [_sReady]
  177.         int    16h
  178.         jnz    Ready1
  179.         xor    ax, ax
  180.         mov    [sKey], ax
  181.         jmp    Ready2
  182.         }
  183.  
  184. Ready1:
  185.  
  186.     _asm {
  187.         mov            [sKey], ax
  188.         }
  189.  
  190. Ready2:
  191.  
  192.     if ((sKey & 0x00FF) == 0x00E0)
  193.         sKey &= 0xFF00;
  194.     else if (sKey & 0x00FF)
  195.         sKey &= 0x00FF;
  196.  
  197.     return sKey;
  198.  
  199. #elif OS_UNIX
  200.  
  201.     return 0;
  202.                                                     
  203. #elif OS_PM || OS_WINDOWS
  204.  
  205.     return 0;
  206.  
  207. #else
  208.  
  209.     return (INT)kbhit();
  210.  
  211. #endif
  212. }
  213.  
  214.  
  215. //----------------------------------------------------------------------------
  216. //   Description:    Get keyboard state.
  217. //    Parameters:    void
  218. //       Returns:    Keyboard state
  219. //----------------------------------------------------------------------------
  220. USHORT FN_EA KbdState(void)
  221. {
  222. #if OS_DOS
  223.  
  224.     USHORT usState;
  225.     SHORT _sState = sState;
  226.     _asm {
  227.         mov    ah, BYTE PTR [_sState]
  228.         int    16h
  229.         mov    [usState], ax
  230.         }
  231.  
  232.     if (!fExtended)
  233.         usState &= 0x00FF;
  234.  
  235.     return usState;
  236.  
  237. #else
  238.     return 0;
  239. #endif
  240. }
  241.  
  242.  
  243. //----------------------------------------------------------------------------
  244. //   Description:    Run standard test suite
  245. //    Parameters:    sTest        Test to run.
  246. //                                        0        Run all default tests (except).
  247. //       Returns:    TRUE if successful.
  248. //----------------------------------------------------------------------------
  249. #if COMPILE_TEST
  250. BOOL FN KbdTest(SHORT sTest)
  251. {
  252.     SHORT sKey;
  253.     USHORT usState;
  254.  
  255.     NOTUSED(sTest);
  256.  
  257.     KbdFlush();
  258.  
  259.     Output("Insert mode is %d.\n", KbdIsInsert());
  260.  
  261.     while (!(sKey = KbdReady()))
  262.         ;
  263.  
  264.     sKey = KbdChar();
  265.     Output("Character = %c (%04X)\n", sKey, sKey);
  266.  
  267. #if OS_UNIX == 0
  268.     Output("Press LEFT shift to continue:\n");
  269.     do
  270.         {
  271.         usState = KbdState();
  272.         }
  273.     while (!(usState & LEFTSHIFT));
  274. #endif
  275.  
  276.     return TRUE;
  277. }
  278. #endif
  279. //----------------------------------------------------------------------------
  280. //------------------------------- End of File --------------------------------
  281. //----------------------------------------------------------------------------
  282.